package com.reference;

import java.lang.ref.PhantomReference;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

/**
 * 引用类型demo
 */
public class ReferenceDemo {
    public static void main(String[] args) {
        ReferenceDemo referenceDemo = new ReferenceDemo();
        /**
         *强引用，永不回收
         */
        ReferenceDemo ref = referenceDemo;
        //ref不用时gc也不回收map 中的值
        Map<ReferenceDemo, Integer> map = new HashMap<ReferenceDemo, Integer>() {{
            put(ref, 1);
        }};
        /**
         *软引用，即将oom时回收
         */
        SoftReference<ReferenceDemo> softReference = new SoftReference<>(referenceDemo);
        //ref不用时gc回收map中的值
        WeakHashMap<ReferenceDemo, Integer> objectObjectWeakHashMap = new WeakHashMap<>();
        /**
         *弱引用，YGC回收
         */
        WeakReference<ReferenceDemo> referenceDemoWeakReference = new WeakReference<>(referenceDemo);
        /**
         *虚引入，即时失效
         */
        PhantomReference<ReferenceDemo> referenceDemoPhantomReference = new PhantomReference<>(referenceDemo, null);
    }
}
